package com.example.joblinker.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.dto.ResumeViewDTO;
import com.example.joblinker.entity.ResumeView;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 简历查看记录Mapper接口
 */
@Mapper
public interface ResumeViewMapper extends BaseMapper<ResumeView> {

    /**
     * 获取用户简历的被查看记录
     *
     * @param page 分页参数
     * @param userId 用户ID
     * @return 简历被查看记录列表
     */
    @Select("SELECT " +
            "   rv.id, " +
            "   rv.resume_id AS resumeId, " +
            "   r.title AS resumeTitle, " +
            "   rv.viewer_id AS viewerId, " +
            "   u.username AS viewerName, " +
            "   c.name AS viewerCompany, " +
            "   rv.view_time AS viewTime " +
            "FROM resume_views rv " +
            "JOIN resume r ON rv.resume_id = r.id " +
            "JOIN user u ON rv.viewer_id = u.id " +
            "LEFT JOIN company c ON u.company_id = c.id " +
            "WHERE r.user_id = #{userId} " +
            "ORDER BY rv.view_time DESC")
    Page<ResumeViewDTO> findViewsByUserId(Page<?> page, @Param("userId") Long userId);

    /**
     * 统计用户简历被查看的次数
     *
     * @param userId 用户ID
     * @return 查看次数
     */
    @Select("SELECT COUNT(*) FROM resume_views rv " +
            "JOIN resume r ON rv.resume_id = r.id " +
            "WHERE r.user_id = #{userId}")
    Integer countByUserId(@Param("userId") Long userId);
} 